package com.educate.serviceedu.controller.front;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.educate.commonutils.JwtUtils;
import com.educate.commonutils.ResultOne;
import com.educate.commonutils.ordervo.CourseWebVoOrder;
import com.educate.serviceedu.client.OrderClient;
import com.educate.serviceedu.entity.EduCourse;
import com.educate.serviceedu.entity.chapter.ChapterVo;
import com.educate.serviceedu.entity.frontvo.CourseFrontVo;
import com.educate.serviceedu.entity.frontvo.CourseWebVo;
import com.educate.serviceedu.entity.vo.CourseQuery;
import com.educate.serviceedu.service.EduChapterService;
import com.educate.serviceedu.service.EduCourseService;
import com.educate.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;
    //1条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public ResultOne getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                        @RequestBody(required = false)CourseFrontVo courseFrontVo){

        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map= eduCourseService.getCourseFrontList(pageCourse,courseFrontVo);

        return ResultOne.ok().data(map);

    }
    //获得课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public ResultOne getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id,编写sql语句查询课程信息
        CourseWebVo courseWebVo= eduCourseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoById(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        Boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return ResultOne.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }


    //根据课程id查询课程的信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
